package com.grudesign.stutrip.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.grudesign.stutrip.common.ReturnInfo;
import com.grudesign.stutrip.entity.Po.NewinfoPo;
import com.grudesign.stutrip.entity.Vo.newsQueryVo;
import com.grudesign.stutrip.mapper.NewinfoMapper;
import com.grudesign.stutrip.service.NewInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 咨询表 服务实现类
 * </p>
 *
 * @author wx
 * @since 2022-05-09
 */
@Service
public class NewinfoServiceImpl extends ServiceImpl<NewinfoMapper, NewinfoPo> implements NewInfoService {

    @Resource
    private NewinfoMapper newinfoMapper;
    @Override
    public Object getNewsList() {
        QueryWrapper<NewinfoPo> newsWrapper = Wrappers.query();
        newsWrapper.orderByDesc("create_date");
        newsWrapper.last("limit 4");
        List<NewinfoPo> newsList = baseMapper.selectList(newsWrapper);
        return newsList;
    }

    @Override
    public Object getSource() {
        return newinfoMapper.getSource();
    }

    @Override
    public Object queryNews(String cur, String li, newsQueryVo queryVo) {
        Page<NewinfoPo> page = new Page<>(Long.parseLong(cur),Long.parseLong(li));
        if(null==queryVo){
            Page<NewinfoPo> newsPage = baseMapper.selectPage(page, Wrappers.emptyWrapper());
            return new ReturnInfo("200","ok").data("newsInfo",newsPage);
        }
        String src = queryVo.getSource();
        String title = queryVo.getTitle();
        QueryWrapper<NewinfoPo> newsWrapper = Wrappers.query();
        if(StringUtils.hasText(src)){
            newsWrapper.eq("source",src);
        }
        if(StringUtils.hasText(title)){
            newsWrapper.like("title",title);
        }
        Page<NewinfoPo> newsPage = baseMapper.selectPage(page, newsWrapper);
        return new ReturnInfo("200","ok").data("newsInfo",newsPage);
    }
}
